import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author Brian added Text Area and Combo Box to Panel and Frame
 * also coded file open save feature. 
 * , Ridwan Started the createToolbars() function, edited it as well.
 */


@SuppressWarnings("serial")
public class GUI extends JFrame{

	public static final int WIDTH = 1200; 
	public static final int HEIGHT = 700;

	public JFrame theWindow;

	private JMenuBar myMenuBar;

	private JMenuItem exit;
	private JMenuItem save;
	private JMenuItem open;
	private JMenuItem newDraft;
	//private JMenuItem showAllDrafts;

	private JFileChooser myFilePicker;
	private FileAction myFileAction;

	private List<Image> icons = new ArrayList<Image>();

	private JLabel bg;

	//private DraftFrame df;

	private MainPanel myMain = new MainPanel();
	private WestPanel myWest = new WestPanel();
	private JPanel imagePanel = new JPanel();

	//private Analyst myAnalyst = new Analyst();

	public GUI(){

		icons.add(new ImageIcon(getClass().getResource("/appicon-128.png")).getImage());
		icons.add(new ImageIcon(getClass().getResource("/appicon-64.png")).getImage());
		icons.add(new ImageIcon(getClass().getResource("/appicon-32.png")).getImage());
		icons.add(new ImageIcon(getClass().getResource("/appicon-16.png")).getImage());

		this.theWindow = start();
	}

	/**
	 * Jacob - Added close operation
	 * 
	 * We have two close options. Which one is
	 * preferred?
	 */
	public JFrame start() {

		JFrame theFrame = new JFrame();

		theFrame.setIconImages(icons);
		theFrame.setSize(WIDTH, HEIGHT);
		theFrame.setResizable(false);
		theFrame.setLocationRelativeTo(null);
		theFrame.setTitle("RFP Response Program");

		//add dog image to frame
		bg = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		
		JLabel bg2 = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		JLabel bg3 = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		JLabel bg4 = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		JLabel bg5 = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		JLabel bg6 = new JLabel(new ImageIcon(new ImageIcon(getClass().getResource("/husky-wallpaper.jpg")).getImage()));
		
		imagePanel.add(bg);
		imagePanel.add(bg2);
		imagePanel.add(bg3);
		imagePanel.add(bg4);
		imagePanel.add(bg5);
		imagePanel.add(bg6);
		
		theFrame.add(BorderLayout.SOUTH, imagePanel);
		
		//add menu bar
		theFrame.setJMenuBar(createToolbars());

		theFrame.add(BorderLayout.CENTER, myMain);
		theFrame.add(BorderLayout.WEST, myWest);

		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		comboBox();

		theFrame.setVisible(true);

		//myAnalyst.loadDrafts();

		return theFrame;
	}

	public JMenuBar createToolbars() {

		myMenuBar = new JMenuBar();

		JMenu file = new JMenu("File");

		open = new JMenuItem("Open");
		save = new JMenuItem("Save");
		exit = new JMenuItem("Exit");

		//JMenu viewMenu = new JMenu("View");

		//showAllDrafts = new JMenuItem("My Drafts");
		newDraft = new JMenuItem("New Draft");

		file.add(newDraft);
		file.add(open);
		file.add(save);		
		file.add(exit);

		//viewMenu.add(showAllDrafts);

		myFileAction = new FileAction();

		open.addActionListener(myFileAction);
		save.addActionListener(myFileAction);

		exit();
		newDraft();
		//listOfDrafts();

		myMenuBar.add(file);
		//myMenuBar.add(viewMenu);

		return myMenuBar;
	}
	/**Combo box that populates text area
	 * 
	 * @author Brian
	 */
	public void comboBox() {

		myWest.myComboBox.addActionListener (new ActionListener () {
			public void actionPerformed(ActionEvent e) {

				AnswerBank ab = new AnswerBank();

				String selectedKey = (String) myWest.myComboBox.getSelectedItem();
				List<String> answer = ab.search(selectedKey);

				for (int i = 0; i < answer.size(); i++) {
					myMain.myTextArea.append("\n"+answer.get(i)+"\n");
				}
			}
		});
	}
	/**Event handler to trigger exit or frame close operation
	 * 
	 * @author Brian
	 */
	public void exit() {
		exit.addActionListener (new ActionListener () {
			public void actionPerformed(ActionEvent e) {

				System.exit(0);
			}
		});
	}
	/**Event handler to create new Draft or clear text area
	 * 
	 * @author Brian
	 */
	public void newDraft() {
		newDraft.addActionListener (new ActionListener () {
			public void actionPerformed(ActionEvent e) {

				myMain.myTextArea.setText(" ");
			}
		});
	}
	/**Method used to display a list of drafts.
	 * 
	 *@author Brian 
	 */
	public void listOfDrafts() {

		/*showAllDrafts.addActionListener (new ActionListener () {
			public void actionPerformed(ActionEvent e) {

				try {

					String name = JOptionPane.showInputDialog(null,"Enter an Analyst Name",
							"Search Drafts?",JOptionPane.PLAIN_MESSAGE);

					int index = myAnalyst.getAllDrafts(name);
					
					if (index >=0) {
			
						JOptionPane.showMessageDialog(null, myAnalyst.searchList(index),"Analyst Draft Display",
								JOptionPane.PLAIN_MESSAGE);
					
					}
				 else {}

				} catch (Exception exp) {

					JOptionPane.showMessageDialog(null, "Sorry, :("+"\n"+"No drafts to Display!","Error",
							JOptionPane.PLAIN_MESSAGE);
				}
			}	
		});*/
	}
	/**@author Brian
	 * 
	 * A class used to display open or save file chooser.
	 * 
	 * some code based on TCSS 305 class example.
	 * 
	 * Instructor: Monika Sobolewska
	 */
	public class FileAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent anEvent) {

			if (myFilePicker == null) {

				myFilePicker = new JFileChooser();

			}

			final Object source = anEvent.getSource();

			myFilePicker.resetChoosableFileFilters();

			int select = -1;

			if (source == save) {

				myFilePicker.setFileFilter(new FileNameExtensionFilter(
						"TXT format(*.txt)", "txt"));

				select = myFilePicker.showSaveDialog(null);

			} else if (source == open) {

				myFilePicker.setFileFilter(new FileNameExtensionFilter(
						"TXT format(*.txt)","txt"));

				select = myFilePicker.showOpenDialog(null);
			}

			File result;

			if (select == JFileChooser.APPROVE_OPTION) {

				result = myFilePicker.getSelectedFile();

				if (result == null) {
					return;
				}
				if (source == save) {
					// Now show our dialog
					// Send the information to the analyst
					//df = new DraftFrame(result.toString());
					//df.getFrame().setVisible(true);

					writeDocumentText(result,myMain.myTextArea);

				} else if (source == open) {

					readDocumentText(result, myMain.myTextArea);

				}
			}
		}
	}
	/**@author Brian method that Reads text file into text area
	 * 
	 * code based on TCSS 305 class example.
	 * 
	 * Instructor: Monika Sobolewska
	 */
	public void readDocumentText(File aFile,JTextArea aTextArea) {

		FileReader fileReader;

		try {

			fileReader = new FileReader(aFile);
			aTextArea.read(fileReader, null);
			fileReader.close();

		} catch (IOException ex) {

			System.out.println(ex);
		}

	}
	/**
	 *@author Brian method that writes text area to file
	 *
	 * code based on TCSS 305 class example.
	 * 
	 * Instructor: Monika Sobolewska
	 */
	public void writeDocumentText(File aFile,JTextArea aTextArea) {

		FileWriter fileWriter;

		try {

			fileWriter = new FileWriter(aFile);

			aTextArea.write(fileWriter);
			fileWriter.close();

		} catch (IOException ex) {

			System.out.println(ex);
		}
	}  
}
